using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Boomer.Source.FrameWork.State_and_Screen_Manager.Screen_Manager;
using FrameWork.Source.Texture;
using Boomer.Source.TiledMap;
using FrameWork.Source.Resource_Manager;
using Microsoft.Xna.Framework;
using Boomer.Source.Character;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;
using Boomer.Source.Boom;
using Boomer.Source.QuadTree;
using FrameWork.Source.Global_variables;
using Boomer.Source.Breakable_Object;
using Boomer.Source.FrameWork.FileMap;
using FrameWork.Source.Screen;
using Microsoft.Xna.Framework.Media;

namespace Boomer.Source.FrameWork.State_and_Screen_Manager.State.MainState.MainScreen
{
    class MainGameScreen : IScreen
    {
        CTiledMap m_pMap;
        CPlayer m_pPlayer;
        CQuadTree m_pQuadTree;
        CTexture m_pBG;
        SpriteFont gameFont;

        public MainGameScreen(int index, string strScreenName, bool isActive, ScreenManager manager)
            : base(index, strScreenName, isActive, manager)
        {
            m_pMap = null;
            m_pPlayer = null;
            m_pQuadTree = null;
            m_pBG = null;
            gameFont = null;
        }

        public override void init()
        {
            m_pQuadTree = new CQuadTree(10, CGLOBAL.g_iScreenWidth, CGLOBAL.g_iScreenHeight);
            m_pMap = new CTiledMap();

            m_pMap.initMap(10, 10, 50);
            m_pMap.initTiledImage(CResourceManager.getInstance().m_pListImage[(int)CResourceManager.IMG_MAIN.MAP_OBJECT], 50, 83);
            m_pMap.setMapPos(new Vector2(50, 50));

            int[,] b = new int[10, 10] {    { 0, 0, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0  }, 
                                            { 0, 0, 0 , 0 , 0 , 0 , 11 , 5 , 5 , 7  } , 
                                            { 11, 6, 0 , 4 , 0 , 0 , 4 , 11 , 11 , 6  } ,
                                            { 5, 5, 0 , 4 , 0 , 11 , 4 , 11 , 11 , 11  } ,
                                            { 11, 0, 0 , 4 , 11 , 11 , 4 , 0 , 0 , 0  } ,
                                            { 11, 11, 0 , 4 , 0 , 0 , 4 , 0 , 0 , 0  } ,
                                            { 0, 0, 0 , 4 , 4 , 4 , 4 , 0 , 0 , 0  } ,
                                            { 0, 7, 11 , 0 , 4 , 4 , 0 , 0 , 7 , 0  } ,
                                            { 0, 5, 5 , 11 , 0 , 0 , 0 , 7 , 7 , 11  } ,
                                            { 0, 0, 0 , 11 , 11 , 0 , 4 , 6 , 0 , 0  } };

            int[,] c = new int[10, 10] {    { 0, 0, 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0  }, 
                                            { 7, 0, 4 , 4 , 4 , 4 , 4 , 4 , 0 , 0  } , 
                                            { 0, 0, 11 , 4 , 4 , 4 , 4 , 11 , 5 , 0  } ,
                                            { 11, 0, 11 , 0 , 0 , 4 , 11 , 11 , 5 , 0  } ,
                                            { 5, 11, 0 , 0 , 4 , 4 , 11 , 11 , 7 , 0  } ,
                                            { 11, 11, 11 , 0 , 4 , 4 , 0 , 0 , 6 , 11  } ,
                                            { 0, 0, 0 , 0 , 4 , 4 , 0 , 0 , 0 , 11  } ,
                                            { 0, 11, 11 , 4 , 4 , 4 , 4 , 11 , 0 , 0  } ,
                                            { 0, 6, 4 , 4 , 4 , 4 , 4 , 4 , 0 , 0  } ,
                                            { 0, 0, 0 , 11 , 0 , 11 , 0 , 0 , 0 , 0  } };

            int[,] a = new int[10, 10] {    { 0, 4, 4 , 4 , 4 , 4 , 4 , 4 , 4 , 0  }, 
                                            { 0, 11, 11 , 4 , 4 , 4 , 4 , 11 , 11 , 0  } , 
                                            { 0, 0, 0 , 11 , 4 , 4 , 0 , 0 , 0 , 0  } ,
                                            { 0, 5, 11 , 0 , 0 , 4 , 11 , 5 , 6 , 5  } ,
                                            { 11, 11, 0 , 0 , 4 , 4 , 11 , 7 , 11 , 11  } ,
                                            { 11, 11, 0 , 0 , 4 , 4 , 0 , 0 , 4 , 0  } ,
                                            { 4, 0, 0 , 6 , 4 , 4 , 0 , 0 , 0 , 0  } ,
                                            { 0, 0, 0 , 0 , 4 , 4 , 0 , 7 , 11 , 0  } ,
                                            { 0, 11, 0 , 11 , 4 , 4 , 11 , 11 , 5 , 0  } ,
                                            { 0, 6, 7 , 11 , 4 , 4 , 11 , 0 , 0 , 0  } };

            CReadFile read = new CReadFile();
            read.ReadMatrix("Map3.txt", a);
            for (int i = 0; i < 10; ++i)
            {
                for (int j = 0; j < 10; ++j)
                {
                    m_pMap.setCell(i, j, a[i, j]);
                    if (a[i, j] == 11)
                    {
                        CBreakableObject breakableObj = new CBreakableObject();
                        breakableObj.init();
                        breakableObj.attachMap(m_pMap, i, j, a[i, j]);
                        ObjectManager.getInstance().addObject(breakableObj);
                    }
                }
            }



            m_pPlayer = new CPlayer();
            m_pPlayer.init();
            m_pPlayer.attachMap(m_pMap);
            m_pPlayer.setPosInMap(3, 4);

            

            BoomManager.getInstance().attachMap(m_pMap);
            WaveManager.WaveManager.getInstance().attachMap(m_pMap);
            WaveManager.WaveManager.getInstance().attachQuadTree(m_pQuadTree);
            WaveManager.WaveManager.getInstance().setPos(0, 0);
            WaveManager.WaveManager.getInstance().setPos(0, 9);
            WaveManager.WaveManager.getInstance().setPos(9, 0);
            WaveManager.WaveManager.getInstance().setPos(9, 9);

            m_pQuadTree.addObject(m_pPlayer);


            m_pBG = CResourceManager.getInstance().getImage((int)CResourceManager.IMG_MAIN.BG);
            gameFont = this.m_pScreenManager.m_pStateManager.m_pGame.Content.Load<SpriteFont>("OtherRes//gameFont");

            if (CGLOBAL.g_isPlayingMusic)
                MediaPlayer.Play(Sound_Manager.SoundManager.getInstance().mainTheme);
            else
                MediaPlayer.Stop();

            base.init();
        }

        public override void update(float deltaTime)
        {
            BoomManager.getInstance().update(deltaTime);
            ObjectManager.getInstance().update(deltaTime);
            Effect.EffectManager.getInstance().update(deltaTime);
            WaveManager.WaveManager.getInstance().update(deltaTime);
            m_pQuadTree.update(deltaTime);

            if (m_pPlayer.m_isAlive == false)
            {
                CGLOBAL.g_isEndGame = true;
                this.m_pScreenManager.m_pStateManager.setNextState(new LoseState(m_pScreenManager.m_pStateManager));
            }
            base.update(deltaTime);
        }

        public override void render()
        {
            

            this.m_pScreenManager.m_pStateManager.m_pSpriteHandler.Begin();

            m_pBG.drawImage(new Vector2(0, 0), Color.White);
            this.m_pScreenManager.m_pStateManager.m_pSpriteHandler.DrawString(gameFont, WaveManager.WaveManager.getInstance().currentWave.ToString(), new Vector2(731, 11), Color.White);
            this.m_pScreenManager.m_pStateManager.m_pSpriteHandler.DrawString(gameFont, m_pPlayer.def.ToString(), new Vector2(731, 111), Color.White);
            this.m_pScreenManager.m_pStateManager.m_pSpriteHandler.DrawString(gameFont, m_pPlayer.numBoom.ToString(), new Vector2(731, 188), Color.White);
            this.m_pScreenManager.m_pStateManager.m_pSpriteHandler.DrawString(gameFont, m_pPlayer.numActiveBoom.ToString(), new Vector2(731, 273), Color.White);
            this.m_pScreenManager.m_pStateManager.m_pSpriteHandler.DrawString(gameFont, m_pPlayer.boomStr.ToString(), new Vector2(731, 358), Color.White);
            this.m_pScreenManager.m_pStateManager.m_pSpriteHandler.DrawString(gameFont, m_pPlayer.speed.ToString(), new Vector2(715, 437), Color.White);

            this.m_pScreenManager.m_pStateManager.m_pSpriteHandler.End();


            this.m_pScreenManager.m_pStateManager.m_pSpriteHandler.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);

            BoomManager.getInstance().draw();
            ObjectManager.getInstance().draw();
            Effect.EffectManager.getInstance().draw();
            WaveManager.WaveManager.getInstance().draw();
            m_pMap.render();
            m_pQuadTree.draw();

            this.m_pScreenManager.m_pStateManager.m_pSpriteHandler.End();

            base.render();
        }

        public override void processInput(float deltaTime)
        {
            KeyboardState currKeyState = Keyboard.GetState();

            if (currKeyState.IsKeyDown(Keys.Escape))
            {
                this.m_isNotUpdate = true;
                this.m_pScreenManager.setScreen("menu" , true , true);
            }
            base.processInput(deltaTime);
        }
    }
}
